﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net.Mime;

public partial class UserForgetPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Attributes.Add("onclick", "return CheckUserID()");
    }

    private string MailPassword = "password@password";
    private string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
    private int PasswordLength = 6;
    private string CreateRandomPassword(int PasswordLength)
    {
        Random rNum = new Random();
        string NewPassWord = "";
        for (int i = 0; i < PasswordLength; i++)
        {
            NewPassWord += allowedChars[rNum.Next(allowedChars.Length)];
        }
        return NewPassWord;
    } 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_User_SELECTBYID", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@User_ID", SqlDbType.VarChar).Value = txtUserID.Text.Trim();

            SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
            oDataAdapter.Fill(dt);
            oDataAdapter = null;

            if (dt.Rows.Count > 0)
            {
                try
                {
                    string NewPassword = CreateRandomPassword(PasswordLength);

                    MailMessage SendMail = new MailMessage();

                    SendMail.To.Add(dt.Rows[0]["User_Email"].ToString());//(dt.Rows[0]["User_Email"].ToString())
                    SendMail.From = new MailAddress("nattapon@bluescapeasia.com");
                    SendMail.Subject = "Forgot Password";
                    SendMail.IsBodyHtml = true;
                    SendMail.Body = "Your request we have generated a new password for your account. <br> " +
                                    "Username: " + dt.Rows[0]["User_ID"].ToString() + " <br> " +
                                    "Password: " + NewPassword + " <br> " +
                                    "------------------------------------------- <br> " +
                                    ".:: Poximo.com ::.";
                    SmtpClient SmtpMail = new SmtpClient("smtp.gmail.com",587);//smtp.gmail.com ,587
                    SmtpMail.Credentials = new System.Net.NetworkCredential("nattapon@bluescapeasia.com", MailPassword);
                    SmtpMail.EnableSsl = true;
                    SmtpMail.Send(SendMail);

                    SendMail = null;

                    string strNewPassword = Security.CipherCode(NewPassword);

                    oCommand = new SqlCommand("sp_Deal_User_UPDATE_PASSWORD", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@User_ID", SqlDbType.VarChar).Value = txtUserID.Text.Trim();
                    oCommand.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = strNewPassword;
                    oCommand.ExecuteNonQuery();

                    trMessage.Visible = true;
                    lblMessage.Text = "Your password send successful.";
                }
                catch (Exception ex)
                {
                    trMessage.Visible = true;
                    lblMessage.Text = "<br>" + ex.Message + "<br><br>";
                }
            }
            else
            {
                trMessage.Visible = true;
                lblMessage.Text = "<br>" + "ไม่มีรหัสผู้ใช้นี้ในระบบ" + "<br><br>";
            }
            oDBConn.Disconnect();
        }
        catch (Exception ex)
        {
            trMessage.Visible = true;
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }
}
